<?php


namespace app\admin\controller;


use think\Exception;
use think\Request;

class Bill extends Common
{
    public function index()
    {
        $data = [];
        //搜索
        $company_name = input('post.company_name');
        $data['company_name'] = $company_name;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $model = new \app\admin\model\Company();
        //列表数据
        $list = $model->getList($pageNum, $numPerPage, $data['company_name'],'');
        $data['list'] = $list;
        //数据总数
        $count = $model->getCount($data['company_name'],'');
        $data['count'] = $count;
        return view('', $data);
    }

    public function exhibition() {

        //页码
        $pageNum = 1;
        $company_id = input('param.cid');
        $map['order_status'] = 3;
        $data['is_distribut'] = -1;
        if(Request::instance()->isPost()){
            $data = input('post.');
            if (is_numeric(input('post.is_distribut'))) {
                $data['is_distribut'] = input('post.is_distribut');
            }
            if($data['is_distribut'] != -1 ) {
                $map['is_distribut'] = $data['is_distribut'];
            }
            $map['order_sn'] = ['like',"%{$data['order_sn']}%"];
            if(!empty($data['start_time']) && !empty($data['end_time'])) {
                $map['finish_time'] = ['between time',[$data['start_time'],$data['end_time'].'23:59:59']];
            } elseif (!empty($data['start_time']) && empty($data['end_time'])) {
                $map['finish_time'] = ['egt',$data['start_time']];
            } elseif (empty($data['start_time']) && !empty($data['end_time'])) {
                $map['finish_time'] = ['elt',$data['end_time'].'23:59:59'];
            }
            if (is_numeric(input('post.pageNum'))) {
                $pageNum = input('post.pageNum');
            }
        }
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $map['company_id'] =  $company_id;
//        $list = get_list('order',$map,'',$pageNum, $numPerPage,'');
        $list = db('order')
                ->where($map)
                ->page($pageNum,$numPerPage)
                ->order('finish_time desc')
                ->select();
        $count = get_list('order',$map,'','', '','1');
		$sum = db("order")->where($map)->sum("total_amount");
		if(empty($sum)){
			$sum = 0;
		}
		$data['price'] = number_format($sum,2);
        $data['cid'] = $company_id;
        $data['list'] = $list;
        $data['count'] = $count;
        $data['pageNum'] = $pageNum;
        return view('',$data);
    }
    /**
     * 查看订单详情
     */
    public function detail() {
        $map['order_id'] = input('get.id');
        $order_info = get_info('order',$map,'');
        return view('',$order_info);
    }
    /**
     * 修改订单状态
     */
    public function modify() {
        $data = input('get.');
        try {
            doModify('order',$data);
        } catch (Exception $e) {
            return json([
                'statusCode' =>300,
                'message' =>"失败"
            ]);
        }
        return json([
            'statusCode' =>200,
            'message' => "成功",
            'navTabId' => 'order_see',
        ]);
    }
    /**
     * 订单-结算
     */
    public function billjs() {
        $id = input('get.id');
        try {
            $data['is_distribut'] = 1;
            $data['update_time'] = time();
            $data['distribut_time'] = time();
            db("order")->where('order_id',$id)->update($data);
        } catch (Exception $e) {
            return json([
                'statusCode' =>300,
                'message' =>"结算失败"
            ]);
        }
        return json([
            'statusCode' =>200,
            'message' => "结算成功",
        ]);
    }

    /**
     * @return int
     * @throws Exception
     * @throws \think\exception\PDOException
     * 批量结算
     */
    public function jsAll()
    {
        $idString = input("post.ids");
        $id = explode(",",$idString);
        $lengthString = count($id);
        $field['is_distribut'] = 1;
        $field['update_time'] = time();
        $field['distribut_time'] = time();
        if($lengthString == 1){
            $delOne = db("order")
                ->where("order_id",$idString)
                ->update($field);
        }else{
            $delOne = db("order")
                ->whereIn("order_id",$idString)
                ->update($field);
        }
        if($delOne > 0){
            return json([
                'statusCode' =>200,
                'message' => "批量结算成功"
            ]);
        }else{
            return json([
                'statusCode' =>300,
                'message' =>"批量结算失败"
            ]);
        }
    }
}